package speelotheek.models;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import speelotheek.components.Database;

public class Statistieken {

	private String q;
	private String count;
	private double boete = 0.00;
	
	private int babyM = 0;
	private int babyV = 0;
	
	private int dreumesM = 0;
	private int dreumesV = 0;
	
	private int peuterM = 0;
	private int peuterV = 0;
	
	private int kleuterM = 0;
	private int kleuterV = 0;
	
	private int schoolkindM = 0;
	private int schoolkindV = 0;
	
	private int puberM = 0;
	private int puberV = 0;
	
	public Statistieken(){
	}

	private String countQuery(String type){
		
		count = "0";
		
		q = "SELECT COUNT(*) as count FROM " + type;

		ResultSet r = Database.executeSelectQuery(q);
		try{				
			ResultSetMetaData metaData = r.getMetaData();
			while(r.next()){
				count = r.getString("count");
			}
			
		} catch(Exception e){
			System.out.println(e);
		}		
				
		return count;
	}
	
	public String getLeden(){
		return countQuery("lid");
	}
	
	public String getKinderen(){
		return countQuery("kind");
	}
	
	public String getSpeelgoed(){
		return countQuery("speelgoed");
	}
	
	public String getUitleningen(){
		return countQuery("uitlening");
	}
	
	public double getBoetes(){
		
		q = "SELECT * FROM uitlening";

		ResultSet r = Database.executeSelectQuery(q);
		try{				

			while(r.next()){
				Date innamedatum     = r.getDate("innamedatum");
				Date terugbrengdatum = r.getDate("terugbrengdatum");
				Date uitleendatum    = r.getDate("uitleendatum");
				
				Uitlening uitlening = new Uitlening();
				//uitlening.setUitleendatum(uitleendatum);
				//uitlening.setInnamedatum(innamedatum);
				//uitlening.setTerugbrengdatum(terugbrengdatum);
				
				
				boete += uitlening.BerekenBoete(innamedatum, terugbrengdatum);
			}
			
		} catch(Exception e){
			System.out.println(e);
		}

		return boete;
	}
	
	public void calculateKids(){
		
		q = "SELECT * FROM kind";

		ResultSet r = Database.executeSelectQuery(q);
		try{				

			while(r.next()){
				String geboortedatum = r.getString("geboortedatum");
				String geslacht      = r.getString("geslacht");
				int leeftijd = getLeeftijd(geboortedatum);
				
				if(leeftijd == 0){
					if(geslacht.equals("M")){
						this.babyM++;
					} else {
						this.babyV++;
					}
				} else if(leeftijd >= 1 && leeftijd < 2 ){
					if(geslacht.equals("M")){
						this.dreumesM++;
					} else {
						this.dreumesV++;
					};
				} else if(leeftijd >= 2 && leeftijd < 4){
					if(geslacht.equals("M")){
						this.peuterM++;
					} else {
						this.peuterV++;
					}
				} else if(leeftijd >= 4 && leeftijd < 6){
					if(geslacht.equals("M")){
						this.kleuterM++;
					} else {
						this.kleuterV++;
					}
				} else if(leeftijd >= 6 && leeftijd < 12){
					if(geslacht.equals("M")){
						this.schoolkindM++;
					} else {
						this.schoolkindV++;
					}
				} else if(leeftijd >= 12 && leeftijd < 18){
					if(geslacht.equals("M")){
						this.puberM++;
					} else {
						this.puberV++;
					}
				}
			}
			
		} catch(Exception e){
			System.out.println(e);
		}
		
	}
	
	private int getLeeftijd(String dob){
		
		//TAKE SUBSTRINGS OF THE DOB SO SPLIT OUT YEAR, MONTH AND DAY
		//INTO SEPERATE VARIABLES
		int yearDOB = Integer.parseInt(dob.substring(0, 4));
		int monthDOB = Integer.parseInt(dob.substring(5, 7));
		int dayDOB = Integer.parseInt(dob.substring(8, 10));

		//CALCULATE THE CURRENT YEAR, MONTH AND DAY
		//INTO SEPERATE VARIABLES
		DateFormat dateFormat = new SimpleDateFormat("yyyy");
		java.util.Date date = new java.util.Date();
		int thisYear = Integer.parseInt(dateFormat.format(date));

		dateFormat = new SimpleDateFormat("MM");
		date = new java.util.Date();
		int thisMonth = Integer.parseInt(dateFormat.format(date));

		dateFormat = new SimpleDateFormat("dd");
		date = new java.util.Date();
		int thisDay = Integer.parseInt(dateFormat.format(date));

		int age = thisYear - yearDOB;

		if(thisMonth < monthDOB){
		age = age - 1;
		}

		if(thisMonth == monthDOB && thisDay < dayDOB){
		age = age - 1;
		}
		
		return age;
	}
	
	public int getBabyM(){
		return this.babyM;
	}
	
	public int getBabyV(){
		return this.babyV;
	}
	
	public int getDreumesM(){
		return this.dreumesM;
	}
	
	public int getDreumesV(){
		return this.dreumesV;
	}
	
	public int getPeuterM(){
		return this.peuterM;
	}
	
	public int getPeuterV(){
		return this.peuterV;
	}
	
	public int getKleuterM(){
		return this.kleuterM;
	}
	
	public int getKleuterV(){
		return this.kleuterV;
	}
	
	public int getSchoolkindM(){
		return this.schoolkindM;
	}
	
	public int getSchoolkindV(){
		return this.schoolkindV;
	}
	
	public int getPuberM(){
		return this.puberM;
	}
	
	public int getPuberV(){
		return this.puberV;
	}
	
}
